package cn.se.localstation.service.impl;

import cn.se.localstation.service.GeocodingService;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

@Service
public class GeocodingServiceImpl implements GeocodingService {
    @Value("${amap.api.key}")
    private String apiKey;

    @Value("${amap.api.url}")
    private String apiUrl;
    public String getAddress(double latitude, double longitude) {
        String url = UriComponentsBuilder.fromHttpUrl(apiUrl)
                .queryParam("location", longitude + "," + latitude)
                .queryParam("key", apiKey)
                .queryParam("radius", 1)  // 设置查询半径，单位米
                .queryParam("extensions", "all")  // 获取详细地址
                .toUriString();

        RestTemplate restTemplate = new RestTemplate();
        String response = restTemplate.getForObject(url, String.class);

        return parseAddress(response);
    }

    private String parseAddress(String response) {
        try {
            // 解析 JSON 响应
            ObjectMapper mapper = new ObjectMapper();
            JsonNode rootNode = mapper.readTree(response);
            JsonNode resultNode = rootNode.path("regeocode");

            // 获取地址信息
            JsonNode addressNode = resultNode.path("addressComponent");
            System.out.println(addressNode);
            StringBuilder address = new StringBuilder();

            // 组合地址
            // 街道信息
            if (addressNode.has("township") && !addressNode.path("township").asText().isEmpty()) {
                address.append(addressNode.path("township").asText()).append(", ");
            }

//            // 小区信息
//            if (addressNode.has("neighborhood") && !addressNode.path("neighborhood").asText().isEmpty()) {
//                address.append(addressNode.path("neighborhood").asText()).append(", ");
//            }
//
//            // 建筑信息
//            if (addressNode.has("building") && !addressNode.path("building").asText().isEmpty()) {
//                address.append(addressNode.path("building").asText()).append(", ");
//            }

            // 区/县信息
            if (addressNode.has("district") && !addressNode.path("district").asText().isEmpty()) {
                address.append(addressNode.path("district").asText()).append(", ");
            } else if (addressNode.has("county") && !addressNode.path("county").asText().isEmpty()) {
                // 如果没有区，可能返回县
                address.append(addressNode.path("county").asText()).append(", ");
            }

            // 城市信息
            if (addressNode.has("city") && !addressNode.path("city").asText().isEmpty()) {
                address.append(addressNode.path("city").asText()).append(", ");
            }

            // 省份信息
            if (addressNode.has("province") && !addressNode.path("province").asText().isEmpty()) {
                address.append(addressNode.path("province").asText());
            }

            // 如果有街道、社区、小区等信息，可以将其精确化
            if (address.length() > 0 && address.charAt(address.length() - 2) == ',') {
                address.delete(address.length() - 2, address.length()); // 删除最后的逗号和空格
            }

            return address.toString().isEmpty() ? "Address not found" : address.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return "Address not found";
        }
    }

}
